﻿<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" href="../themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="../themes/icon.css">
	<link rel="stylesheet" type="text/css" href="../demo.css">
	<script type="text/javascript" src="../jquery.min.js"></script>
	<script type="text/javascript" src="../jquery.cookie.js"></script>
	<script type="text/javascript" src="../jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="../extend.js"></script>
    <script>
        var dataCache = null;
        var appId = GetQueryString("moduleid");
        window.onresize=calSize;
        var rowEditing = -1;
        var fieldEditing = -1;
        var columnsList = [];
        var editing = false;
        var cusId=GetQueryString("cusid");
        var customer =  dataFetcher("../../api/customer/get?id=" + cusId);
		var employees =getEmployee();
        document.onkeydown = keyDown;
		var employeesInSP=getEmployeeBySalesPoint();
        $(document).ready(function(){
			initBase();
            listDetail();
            calSize();
            getColumnVisable();
		});
        function getColumnVisable(){
            var allColumn = $('#salesBonusList').datagrid('getColumnFields');
            for(var j=0;j<allColumn.length;j++){
                var col=$('#salesBonusList').datagrid('getColumnOption',allColumn[j]);
                if(!(col.hidden)&&(col.editor!=undefined)){
                    columnsList.push(allColumn[j]);
        		}
            }
        }
        function updateMoney(){
             customer = dataFetcher("../../api/customer/get?id=" + cusId);
            $('#Received').numberbox('setValue', customer.Received);             
            $('#Receiveable').numberbox('setValue', customer.Receiveable);
            $('#SalesBonus').numberbox('setValue', customer.SalesBonus);
        }
        function initBase(){
            $('#Code').textbox('setValue', customer.Code);
            $('#Name').textbox('setValue', customer.Name);
            $('#SalesId').textbox('setValue', valToName(employeesInSP, customer.SalesId));
            $('#SalesBonus').numberbox('setValue', customer.SalesBonus);             
            $('#SecSalesId').textbox('setValue', valToName(employeesInSP, customer.SecSalesId));
            $('#Source').textbox('setValue', valToName(customerSourceData, customer.Source));
            $('#Received').numberbox('setValue', customer.Received);    
            $('#Receiveable').numberbox('setValue', customer.Receiveable);
            $.get("../../api/salespoint/get?id=" + customer.SalesPointId,function(data){if(data.Result==0)$('#SalesPointId').textbox('setValue',(JSON.parse(data.Data)).Name);},"json");
        }
        function listDetail(){
            $('#salesBonusList').datagrid('loading');
            $.get("../../api/salesbonus/listbycustomer?customer=" + cusId,function(data){
                if(data.Result==0){
					$('#salesBonusList').datagrid({data: JSON.parse(data.Data)}).datagrid('clientPaging'); 
                }
                else{
                    
                }
            },"json")
            .error(function(){
                $.messager.alert("错误","系统内部错误","error");
            })
            .complete(function() { 
                $('#salesBonusList').datagrid('loaded');
            });
        }
        window.onresize=calSize;
        function calSize(){
            var height=window.parent.document.getElementById("mainFrame").clientHeight;
            var width=window.parent.document.getElementById("mainFrame").clientWidth;
            window.parent.document.getElementById(appId).height = height-35;
            $("#salesBonusList").datagrid('resize',{height:height-205,width:width-20});
            $("#toolbar").panel({width:width-20});
            $("#editZone").panel({width:width-20});
        }
        function addItem() {
			var date = new Date().toLocaleDateString();
			if(rowEditing>0){ $('#salesBonusList').datagrid("endEdit",rowEditing); }
			dataCache = { AutoId: 0, Flag: 0, Code:getAutoCode('EXP_SALESBONUS_SET'),Date: date,State:0,CustomerId: cusId ,CalPer:0,BonusBase:0};
			$('#salesBonusList').datagrid('appendRow', dataCache);
        }
        function rowSelected(index,row){
			if(rowEditing!=index){
				$('#salesBonusList').datagrid('validateRow', rowEditing);
				$('#salesBonusList').datagrid("endEdit",rowEditing)
				rowEditing = index;
				dataCache = row;
			}
			if(row.State==1||row.State==0){
				if(!editing){
					$('#salesBonusList').datagrid("enableCellEditing");
					editing = true;
				}
			}else{
				if(editing){
                    $('#salesBonusList').datagrid("endEdit",index);
					$('#salesBonusList').datagrid("disableCellEditing");
					editing = false;
				}
			}
            updateBtnState(dataCache.State);
		}
        function submitChange(){
            showMask();
            var valid = $('#salesBonusList').datagrid('validateRow', rowEditing);
            if (!valid) {
                $.messager.alert("错误", "错误的数据格式", "error");
                return;
            }
            $('#salesBonusList').datagrid('endEdit', rowEditing);
            dataCache = $('#salesBonusList').datagrid('getSelected');
            $.post("../../api/salesbonus/set",dataCache,function(response){
                if(response.Result==0){
                    dataCache = JSON.parse(response.Data);
					$('#salesBonusList').datagrid('updateRow',{index:rowEditing,row:dataCache});		
					$('#salesBonusList').datagrid('refreshRow',rowEditing ); 				
                    updateBtnState(dataCache.State);                    
                    $.messager.alert("信息","操作成功","info");
                }else{
                    $.messager.alert("错误",response.Message,"error");
                }
            })
            .error(function(){
                $.messager.alert("错误","系统内部错误","error");
            })
            .complete(function() { 
                hideMask();
            });
        }
		function deleteRecord(){
			var selected = $('#salesBonusList').datagrid('getSelected');
			if(!selected){
                $.messager.alert("错误", "请先选中要删除的对象", "error");
                return;
            }else{
				dataCache = selected;
			}
			$.messager.confirm('删除提示', '确定删除该记录?', function(r){
				if (r){
					if(dataCache.AutoId&&dataCache.AutoId>0){		
						showMask();					
						$.post("../../api/salesBonus/remove",dataCache,function(response){
							if(response.Result==0){				
								$.messager.alert("信息","操作成功","info");
								$('#salesBonusList').datagrid('cancelEdit', rowEditing).datagrid('deleteRow',rowEditing);
								rowEditing = -1;
							}else{
								$.messager.alert("错误",response.Message,"error");
							}
						})
						.error(function(){
							$.messager.alert("错误","系统内部错误","error");
						})
						.complete(function() { 
							hideMask();
						});	
					}else{
						$('#salesBonusList').datagrid('cancelEdit', rowEditing).datagrid('deleteRow',rowEditing);
						rowEditing = -1;
					}
				}
			});		
		}
		function submitRecord(){
			showMask();
			dataCache = $('#salesBonusList').datagrid('getSelected' ); 
			$.get("../../api/salesbonus/submit?id="+dataCache.AutoId,function(response){
				if(response.Result==0){			
					dataCache.State = 2;
					$('#salesBonusList').datagrid('updateRow',{index:rowEditing,row:dataCache} ); 
					$('#salesBonusList').datagrid('refreshRow',rowEditing ); 
                    updateBtnState(dataCache.State);
					$.messager.alert("信息","操作成功","info");
					}else{
						$.messager.alert("错误",response.Message,"error");
					}
				})
				.error(function(){
					$.messager.alert("错误","系统内部错误","error");
				})
				.complete(function() { 
					hideMask();
				});	
		}
		function recallRecord(){
			showMask();
			dataCache = $('#salesBonusList').datagrid('getSelected' ); 
			$.get("../../api/salesbonus/recall?id="+dataCache.AutoId,function(response){
				if(response.Result==0){
					dataCache.State = 1;
					$('#salesBonusList').datagrid('updateRow', {index:rowEditing,row:dataCache}); 
					$('#salesBonusList').datagrid('refreshRow',rowEditing );
                    updateBtnState(dataCache.State);                    
					$.messager.alert("信息","操作成功","info");
					}else{
						$.messager.alert("错误",response.Message,"error");
					}
				})
				.error(function(){
					$.messager.alert("错误","系统内部错误","error");
				})
				.complete(function() { 
					hideMask();
				});	
			
		}
		function approveRecord(){
			showMask();
			dataCache = $('#salesBonusList').datagrid('getSelected' ); 
			$.get("../../api/salesbonus/approve?id="+dataCache.AutoId,function(response){
				if(response.Result==0){
					dataCache.State = 3;
					$('#salesBonusList').datagrid('updateRow', {index:rowEditing,row:dataCache}); 
					$('#salesBonusList').datagrid('refreshRow',rowEditing ); 
                    updateBtnState(dataCache.State);
                    updateMoney();
					$.messager.alert("信息","操作成功","info");
					}else{
						$.messager.alert("错误",response.Message,"error");
					}
				})
				.error(function(){
					$.messager.alert("错误","系统内部错误","error");
				})
				.complete(function() { 
					hideMask();
				});	
			
		}
		function revertRecord(){
			showMask();
			dataCache = $('#salesBonusList').datagrid('getSelected' ); 
			$.get("../../api/salesbonus/revert?id="+dataCache.AutoId,function(response){
				if(response.Result==0){
					dataCache.State = 1;
					$('#salesBonusList').datagrid('updateRow', {index:rowEditing,row:dataCache}); 
					$('#salesBonusList').datagrid('refreshRow',rowEditing ); 
                    updateBtnState(dataCache.State);
                    updateMoney();
					$.messager.alert("信息","操作成功","info");
					}else{
						$.messager.alert("错误",response.Message,"error");
					}
				})
				.error(function(){
					$.messager.alert("错误","系统内部错误","error");
				})
				.complete(function() { 
					hideMask();
				});	
			
		}
		function rejectRecord(){
			showMask();
			dataCache = $('#salesBonusList').datagrid('getSelected' ); 
			$.get("../../api/salesbonus/reject?id="+dataCache.AutoId,function(response){
				if(response.Result==0){
					dataCache.State = 1;
					$('#salesBonusList').datagrid('updateRow', {index:rowEditing,row:dataCache}); 
					$('#salesBonusList').datagrid('refreshRow',rowEditing ); 
                    updateBtnState(dataCache.State);
					$.messager.alert("信息","操作成功","info");
					}else{
						$.messager.alert("错误",response.Message,"error");
					}
				})
				.error(function(){
					$.messager.alert("错误","系统内部错误","error");
				})
				.complete(function() { 
					hideMask();
				});	
		}
        function salesFormater(value, row, index){
            return valToName(employeesInSP,value);
        }
        function perFormater(value){
            return parseFloat(value)*100.0+'%';
        }
        function calBonusAfterCalBaseChanged(n,o){
            var row = $('#salesBonusList').datagrid('getSelected');
            row.Bonus = n*row.CalPer*row.BonusBase;
        }
        function calBonusAfterCalPerChanged(n,o){
            var row = $('#salesBonusList').datagrid('getSelected');
            row.Bonus = n*row.CalBase*row.BonusBase;
        }
        function calBonusAfterBonusBaseChanged(n,o){
            var row = $('#salesBonusList').datagrid('getSelected');
            row.Bonus = n*row.CalPer*row.CalBase;
        }
        function setCellEditable(){
            $('#salesBonusList').datagrid('validateRow', rowEditing);
            $('#salesBonusList').datagrid('endEdit', rowEditing);
            $('#salesBonusList').datagrid('selectRow', rowEditing).datagrid('editCell', {index: rowEditing,field: fieldEditing});
			var ed=$('#salesBonusList').datagrid('getEditor', {index:rowEditing,field:fieldEditing});
        }
        function keyDown(event) {
            if(!editing){
                return;
            }
            var e = event || window.event;
            var keyCode = e.keyCode || e.which;
            switch (keyCode) {
                case 37:
                    hMove(-1);
                    break;
                case 39:
                    hMove(1);
                    break;
            }
        }
        function hMove(direction){
            if(rowEditing==-1){
                return;
            }
            if(fieldEditing==-1){
                fieldEditing = columnsList[0];
            }
            var ed=$('#salesBonusList').datagrid('getEditor', {index:rowEditing,field:fieldEditing});
            if(!ed){
                return;
            }
            if(direction>=0){
                direction=1;
            }
            else{
                direction=-1;
            }
            var tag = null;
            if($(ed.target).data('textbox')){
                tag = $(ed.target).textbox('textbox')[0];
            }
            var valueLenth=tag.value.length;
            if(tag==null||tag.readOnly||(tag.selectionStart==0 || tag.selectionStart==undefined)&&direction<0
                ||(tag.selectionEnd==valueLenth || tag.selectionEnd==undefined)&&direction>=0){
                for(var i=0;i<columnsList.length;i++){
                    if(fieldEditing==columnsList[i] && (i+direction)>=0 && (i+direction)<columnsList.length){
                        fieldEditing=columnsList[(i+direction)];
                        break;
                    }
                }
                setCellEditable();
            }
        }
        function setEditingLocation(index, field){
            if(!editing){
                return;
            }
            var editor = $('#salesBonusList').datagrid('getEditor', { index: index, field: field });
            if(editor!=null){
                rowEditing=index;
                fieldEditing=field;
            }
        }
    </script>
</head>
<body onload = "hideMask()">
    <script>showMask('正在载入数据');</script>
    <div id="toolbar" class="easyui-panel" style="padding:1px;">
        <a href="#" id="detailAdd" class="easyui-linkbutton" data-options="onClick:addItem,iconCls:'icon-add',plain:true">创建</a>
        <a href="#" id="detailDel" class="easyui-linkbutton" data-options="onClick:deleteRecord,iconCls:'icon-remove',plain:true,disabled:true">删除</a>
        <a href="#" id="detailSave" class="easyui-linkbutton" data-options="onClick:submitChange,iconCls:'icon-save',plain:true,disabled:true">保存</a>
        <a href="#" id="btnSubmit" class="easyui-linkbutton" data-options="onClick:submitRecord,iconCls:'icon-redo',plain:true,disabled:true">提审</a>
        <a href="#" id="btnRecall" class="easyui-linkbutton" data-options="onClick:recallRecord,iconCls:'icon-undo',plain:true,disabled:true">撤回</a>
        <a href="#" id="btnApprove" class="easyui-linkbutton" data-options="onClick:approveRecord,iconCls:'icon-ok',plain:true,disabled:true">审核</a>
        <a href="#" id="btnRevert" class="easyui-linkbutton" data-options="onClick:revertRecord,iconCls:'icon-cancel',plain:true,disabled:true">反审</a>
        <a href="#" id="btnReject" class="easyui-linkbutton" data-options="onClick:rejectRecord,iconCls:'icon-clear',plain:true,disabled:true">退回</a>
        <a href="#" class="easyui-linkbutton" data-options="onClick:function(){exportData('../../api/salesbonus/export',[{CustomerId:cusId}])} ,iconCls:'icon-pencil',plain:true">导出</a>
    </div>
    <div style="margin:2px"></div>
    <div id="editZone" class="easyui-panel" style="padding:10px;">
        <table>
            <tr>
                <td><input class="easyui-textbox" id="Code" style="width:300px" data-options="label:'客户编号',editable:false"></td>
                <td>&nbsp;</td>
                <td><input class="easyui-textbox" id="Name" style="width:300px" data-options="label:'客户名称:',editable:false"></td>
				<td>&nbsp;</td>
				<td><input class="easyui-textbox" id="SalesPointId" style="width:300px" data-options="label:'门店',editable:false"></td>
            </tr>
            <tr>
                <td><input class="easyui-textbox" id="Source" style="width:300px" data-options="label:'来源',editable:false"></td>
                <td>&nbsp;</td>
                <td><input class="easyui-textbox" id="SalesId" style="width:300px" data-options="label:'业务员:',editable:false"></td>
				<td>&nbsp;</td>
                <td><input class="easyui-textbox" id="SecSalesId" style="width:300px" data-options="label:'导购:',editable:false,"></td>
            </tr>
            <tr>
                <td><input class="easyui-numberbox" id="Receiveable" style="width:300px" data-options="label:'客户应收',editable:false,editable:false,precision:2,groupSeparator:',',decimalSeparator:'.',prefix:'￥'"></td>
                <td>&nbsp;</td>
                <td><input class="easyui-numberbox" id="Received" style="width:300px" data-options="label:'客户已收:',editable:false,editable:false,precision:2,groupSeparator:',',decimalSeparator:'.',prefix:'￥'"></td>
				<td>&nbsp;</td>
				<td><input class="easyui-numberbox" id="SalesBonus" style="width:300px" data-options="label:'累计计提',editable:false,editable:false,precision:2,groupSeparator:',',decimalSeparator:'.',prefix:'￥'"></td>
            </tr>
        </table>
    </div>
    <div style="margin:2px"></div>                        
    <table id="salesBonusList" class="easyui-datagrid"  data-options="singleSelect:true,method:'get',onClickRow:rowSelected,pagination:true,rownumbers:true,onClickCell:setEditingLocation">
        <thead>
            <tr>
                <th data-options="field:'AutoId',hidden:true"></th>
                <th data-options="field:'Flag',hidden:true"></th>
                <th data-options="field:'CustomerId',hidden:true"></th>
                <th data-options="field:'Code',width:100,align:'right',editor:{type:'textbox'}">费用编号</th>
                <th data-options="field:'SalesId',width:120,align:'right',
                        formatter:salesFormater,
						editor:{
							type:'combogrid',
							options:getComboGridInfo(null,'Name',[[
									{field:'AutoId',title:'ID',width:60},
									{field:'Code',title:'代号',width:60},
									{field:'Name',title:'姓名',width:100}]],employeesInSP)
						}">业务员</th>
                <th data-options="field:'CalBase',width:100,align:'right',formatter:moneyFormatter,editor:{type:'numberbox',options:{precision:2,groupSeparator:',',decimalSeparator:'.',prefix:'￥',onChange:calBonusAfterCalBaseChanged}}">计提基础</th>
                <th data-options="field:'CalPer',width:100,align:'right',formatter:perFormater,editor:{type:'numberbox',options:{precision:4,onChange:calBonusAfterCalPerChanged}}">计提比例</th>
                <th data-options="field:'BonusPer',width:100,align:'right',formatter:perFormater,editor:{type:'numberbox',options:{precision:4,onChange:calBonusAfterBonusBaseChanged}}">提成点数</th>
                <th data-options="field:'Bonus',width:100,align:'right',formatter:moneyFormatter,editor:{type:'numberbox',options:{precision:2,groupSeparator:',',decimalSeparator:'.',prefix:'￥'}}">金额</th>
                <th data-options="field:'Date',width:100,align:'right',editor:{type:'datebox',options:{formatter:dateFormatter,parser:dateParser}}">日期</th>
                <th data-options="field:'State',width:100,formatter:stateFormatter">状态</th>
        </tr>
    </thead>
    </table>
</body>
</html>